package com.bristua.barcode.customer.biz;

import com.bristua.barcode.common.util.JsonUtil;
import com.bristua.barcode.customer.dao.TCustomerBindSupplierMapper;
import com.bristua.barcode.customer.model.TCustomerBindSupplier;
import com.bristua.barcode.customer.model.bo.CustomerBindQRCode;
import com.bristua.barcode.customer.model.condition.AddCustomerBindQRCodeParams;
import com.bristua.barcode.customer.model.condition.TCustomerBindSupplierExample;
import com.bristua.barcode.supplier.dao.TChannelMapper;
import com.bristua.barcode.supplier.dao.TQrCodeLimitAmtAdjustmentMapper;
import com.bristua.barcode.supplier.dao.TQrCodeMapper;
import com.bristua.barcode.supplier.model.TChannel;
import com.bristua.barcode.supplier.model.TQrCode;
import com.bristua.barcode.supplier.model.TQrCodeLimitAmtAdjustment;
import com.bristua.barcode.supplier.model.condition.TQrCodeLimitAmtAdjustmentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/8/9.
 */
@Service
public class CustomerBindQRCodeWriteIntoRedisBiz {

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private TCustomerBindSupplierMapper tCustomerBindSupplierMapper;

    @Autowired
    private GetCustomerBindQRCodeBiz getCustomerBindQRCodeBiz;

    @Resource
    private TQrCodeMapper tQrCodeMapper;

    @Resource
    private TChannelMapper tChannelMapper;

    @Async
    public void customerBindQRCodeWriteIntoRedis(String customerId){
        TCustomerBindSupplierExample tCustomerBindSupplierExample = new TCustomerBindSupplierExample();
        tCustomerBindSupplierExample.createCriteria().andCustomerIdEqualTo(customerId);
        List<TCustomerBindSupplier> tCustomerBindSuppliers = tCustomerBindSupplierMapper.selectByExample(tCustomerBindSupplierExample);
        if(tCustomerBindSuppliers!=null && tCustomerBindSuppliers.size()>0){
            for(TCustomerBindSupplier tCustomerBindSupplier : tCustomerBindSuppliers){
                CustomerBindQRCode customerBindQRCode = getCustomerBindQRCodeBiz.getCustomerBindQRCode(customerId, tCustomerBindSupplier);
                TQrCode tQrCode = tQrCodeMapper.selectByPrimaryKey(tCustomerBindSupplier.getQrCodeId());
                StringBuilder key = new StringBuilder();
                key.append(customerId).append(tQrCode.getChannelId());
                TChannel tChannel = tChannelMapper.selectByPrimaryKey(tQrCode.getChannelId());
                if(tChannel!=null){
                    if(tChannel.getChannelType().intValue()==1 && !StringUtils.isEmpty(tQrCode.getCodeAmt())){
                        key.append(tQrCode.getCodeAmt());
                    }
                }
                redisTemplate.opsForList().leftPush(key.toString(), JsonUtil.toJson(customerBindQRCode));

            }
        }
    }


}
